package dao.impl;

import dao.BaseDao;
import dao.CategoryDao;
import entity.Category;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CategoryDaoImpl extends BaseDao implements CategoryDao {

    @Override
    public List<Category> findAll() {
        String sql="select `id`, `name`, `parent_id`, `icon`, `sort`, `status`, `is_deleted`, `update_time`  from kt_category where is_deleted=0 order by parent_id,sort asc";
        try {
            return executeQuery(sql,rs->{

                    List<Category> list=new ArrayList<Category>();
                    try {
                    while (rs.next()){
                        list.add(extractCategory(rs));
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
                return list;
            });
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int insert(Category category) {
        String sql="INSERT INTO `kmall`.`kt_category` ( `name`, `parent_id`, `icon`, `sort`, `status`) VALUES (?, ?, ?, ?, ?);";
        try {
            return executeUpdate(sql,category.getName(),category.getParentId(),category.getIcon(),category.getSort(),category.getStatus());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int update(Category category) {
        StringBuilder sql=new StringBuilder("UPDATE kt_category SET");
        List<Object> params=new ArrayList<>();
        if (category.getName()!=null){
            sql.append("name=?,");
            params.add(category.getName());
        }
        if (category.getParentId()!=null){
            sql.append("parent_Id=?,");
            params.add(category.getParentId());
        }
        if (category.getIcon()!=null){
            sql.append("icon=?,");
            params.add(category.getIcon());
        }
        if (category.getSort()!=null){
            sql.append("sort=?,");
            params.add(category.getSort());
        }
        if (category.getStatus()!=null){
            sql.append("status=?,");
            params.add(category.getStatus());
        }
        sql.append(category.getId());
        params.add(category.getId());
        try {
            return executeUpdate(sql.toString(),params.toArray());
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }
    }

    @Override
    public int delete(Long id) {
        String sql="UPDATE kt_category set is_delete=1 WHERE id=?";
        try {
            return executeUpdate(sql,id);
        } catch (SQLException e) {
            e.printStackTrace();
            return -1;
        }

    }

    private Category extractCategory(ResultSet rs) {
        Category cat=new Category();
        try {
            cat.setParentId(rs.getString("id"));
            cat.setName(rs.getString("name"));
            cat.setParentId(rs.getString("parent_Id"));
            cat.setIcon(rs.getString("icon"));
            cat.setSort(rs.getInt("sort"));
            cat.setStatus(rs.getInt("status"));
            cat.setIsDeleted(rs.getInt("is_Deleted"));

            return cat;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
